//********************************************************************
// 
//  file:  FileManagement.cs
//  FileManagement Sample Call
//
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.IO;
using data.diomedestorage.com;

namespace Diomede.SDK.Example.Samples
{
    public class FileManagement
    {
        ISession session;
        string username = string.Format("unittestc{0}", DateTime.Now.Ticks);
        string password = "password914";
        
        //string fileToUpload = @"c:\temp\uploads\reef-sharks-and-cage-516496-xl.jpg";

        public FileManagement()
        {
            session = new Session();
        }

        public void Start()
        {
            session = new Session();
            string email = string.Format("{0}@email.com", username);
            IAccountManagement accountManagement = session as IAccountManagement;
            accountManagement.Create(username, password, email);

            if (session.Login(username, password).Status != Diomede.SDK.Enums.ExecutionResults.Success)
            {
                Utils.WriteError("Fail to login to Diomede Storage Service, please check your settings.");
                return;
            }
            //uploadFile();
            //uploadBulk();

            //searchFiles();

            session.Logout();
            Utils.WriteMessage("Upload Complete");
            Console.ReadKey();
        }

        //void uploadFile()
        //{
        //    string fileToUpload = @"c:\mov\FKindom.avi";
        //    IFileManagement fm = session as IFileManagement;
        //    //add Progress event handler
        //    fm.OnFileUploadProgress += new EventHandler<FileUploadProgressArgs>(fm_OnFileUploadProgress);

        //    Utils.WriteMessage(string.Format("Starting to upload {0}..", fileToUpload));
        //    //upload file now
        //    IFileManagementExecResult result = fm.Upload(new FileInfo(fileToUpload), "video/avi", "");
        //    if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
        //    {
        //         Utils.WriteMessage("Upload successful... Press any key");
        //        Console.ReadKey();
        //    }
        //}

        //void uploadBulk()
        //{
        //    IFileManagement fm = session as IFileManagement;
        //    fm.OnFileUploadProgress +=new EventHandler<FileUploadProgressArgs>(fm_OnFileUploadProgress);
        //    fm.OnTotalUploadProgress += new EventHandler<TotalUploadProgressArgs>(fm_OnTotalUploadProgress);

        //    string[] filenames = Directory.GetFiles(@"C:\temp\uploads");
        //    Utils.WriteMessage(@"Uploading all content in folder c:\temp\uploads");
        //    IFileManagementBulkUploadExecResult result = fm.Upload(filenames, Diomede.SDK.Enums.StorageInfo.OnlineStorage);
        //    Utils.WriteMessage(string.Format("{0}... Press any key", result.Message));
        //    Console.ReadKey();
        //}

        string fileProgress = "";
        void fm_OnTotalUploadProgress(object sender, TotalUploadProgressArgs e)
        {
            //fileProgress = e.ToProgressString();
            Console.WriteLine("Batch uploading progress, " + e.ToProgressString());
        }

        void fm_OnFileUploadProgress(object sender, FileUploadProgressArgs e)
        {
            Console.Clear();
            Utils.WriteError(fileProgress);
            Utils.WriteMessage(string.Format("Upload progress... {0} / {1}", e.ToProgressString(), e.ToPercentString()));
        }


        void searchFiles()
        {
            IFileManagement fm = session as IFileManagement;

            DateTime dateStart = new DateTime(2008, 1, 1);
            DateTime dateEnd = DateTime.Now;

            IFileManagementExecResult result = fm.SearchFiles(null, string.Empty, string.Empty, string.Empty, null, null, dateStart, dateEnd, null,false, null, null, 0, 20);
            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                result.StoredFiles.ForEach(delegate(IStoredFile file) {
                    Utils.WriteMessage(string.Format("{0} - {1}", file.FileInformation.fileName, file.GetDownloadUrl(5,null,null,"")));
                });
            }
            Utils.WriteMessage("Press any key");
            Console.ReadKey();
        }

        //static void uploadFilE()
        //{
        //    ISession session = new Session();

        //    if (session.Login("username", "password").Status != Diomede.SDK.Enums.ExecutionResults.Success)
        //    {
        //        Console.WriteLine("Fail to login to Diomede Service.");
        //        return;
        //    }
        //    string fileToUpload = @"c:\tempfile\toupload.zip";

        //    IFileManagement fileMgr = session as IFileManagement;
        //    //add Progress event handler
        //    fileMgr.OnFileUploadProgress += new EventHandler<FileUploadProgressArgs>(fileMgr_OnFileUploadProgress);
        //    Utils.WriteMessage(string.Format("Starting to upload {0}..", fileToUpload));
        //    //upload file now
        //    IFileManagementExecResult result = fileMgr.Upload(fileToUpload, Diomede.SDK.Enums.StorageInfo.OnlineStorage);
        //    if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
        //    {
        //        Utils.WriteMessage("Upload successful... Press any key");
        //        Console.ReadKey();
        //    }

        //}

        static void fileMgr_OnFileUploadProgress(object sender, FileUploadProgressArgs e)
        {
            Console.WriteLine(string.Format("Upload progress... {0} / {1}", e.ToProgressString(), e.ToPercentString()));
        }
    }
}